package com.sc.base.user.service;

import com.sc.base.user.bean.entity.User;
import com.sc.base.user.bean.pojo.UserTest;
import com.sc.commons.base.dto.BasePageReq;
import com.sc.commons.base.repository.BaseRepository;
import com.sc.commons.base.repository.BaseRepositoryImpl;
import com.sc.commons.base.repository.JpaQueryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author
 */
@Service
public class UserService {

    @Autowired
    private BaseRepository<User> userBaseRepository;


    @Transactional(rollbackFor = Exception.class)
    public void save(){
        User u = new User();
        u.setUsername("马凯");
        u.setBirthDayDate(LocalDate.of(1989,12,23));
        userBaseRepository.save(u);
    }

    @Transactional(readOnly = true)
    public void getEntity() {
        User byId = userBaseRepository.getEntity(User.class, 1L);
        System.out.println(byId.getUsername());
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(){
        User u = userBaseRepository.getEntity(User.class, 1L);
        u.setUsername("我是谁");
        u.setBirthDayDate(LocalDate.of(1989,12,23));
        userBaseRepository.update(u);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(){
        User u = userBaseRepository.getEntity(User.class, 1L);
        userBaseRepository.delete(u);
    }


    @Transactional(readOnly = true)
    public void getEntityTest() {
        // 占位符式查询
        User entity = userBaseRepository.getEntity(" from User where bid = ?1 ", 2L);
        System.out.println(entity.getUsername());


        // 命名查询
        Map<String, Object> params = new HashMap<>();
        params.put("bid", 2L);
        User u = userBaseRepository.getEntity(" from User where bid = :bid ", params);
        System.out.println(u.getUsername());

        // 无条件查询
        User from_user = userBaseRepository.getEntity("from User");
        System.out.println(from_user);

        // 通过in查询，参数为集合
        User entity1 = userBaseRepository.getEntity("from User where bid in (?1)", Arrays.asList(1L, 2L, 3L, 4L));
        System.out.println(entity1);

    }

    @Transactional(readOnly = true)
    public void getTest() {
//            UserTest userTest = userBaseRepository.get(UserTest.class, "select bid as bid, username as username, age as age from User");
//            UserTest userTest = userBaseRepository.get(UserTest.class, "select new com.sc.base.user.bean.pojo.UserTest(bid, username, age) from User");
//            UserTest userTest = userBaseRepository.get(UserTest.class, "select new com.sc.base.user.bean.pojo.UserTest(bid, username, age) from User where bid = ?1", 2L);
        UserTest userTest = userBaseRepository.get(UserTest.class, "select new com.sc.base.user.bean.pojo.UserTest(bid, username, age) from User where bid = :bid", new HashMap<>(){{
            put("bid", 2L);
        }});
        System.out.println(userTest.getBid());

    }

    @Transactional(readOnly = true)
    public void findEntity() {
//        List<User> entity = userBaseRepository.findEntity("from User ");
//        List<User> entity = userBaseRepository.findEntity("from User where bid > ?1", 0L);
        BasePageReq page = new BasePageReq();
        page.setPage(2);
        page.setRows(2);
//        List<User> entity = userBaseRepository.findEntity(page,"from User where bid > ?1", 0L);
//        List<User> entity = userBaseRepository.findEntity("from User where bid > :bid", new HashMap<>(){{
//            put("bid", 1L);
//        }});
        List<User> entity = userBaseRepository.findEntity(page,"from User where bid > :bid", new HashMap<>(){{
            put("bid", 1L);
        }});
        System.out.println(entity.size());



    }

    @Transactional(readOnly = true)
    public void find() {
        BasePageReq page = new BasePageReq();
        page.setPage(1);
        page.setRows(2);

        Map<String, Object> params = new HashMap<>() {{
            put("bid", 1L);
        }};

//        List<UserTest> userTest = userBaseRepository.find(UserTest.class, "select new com.sc.base.user.bean.pojo.UserTest(bid, username, age) from User " );
//        List<UserTest> userTest = userBaseRepository.find(UserTest.class, "select new com.sc.base.user.bean.pojo.UserTest(bid, username, age) from User where bid in (?1)",  new ArrayList<>(){{add(2L);add(3L);}});
//        List<UserTest> userTest = userBaseRepository.find(UserTest.class,  page, "select new com.sc.base.user.bean.pojo.UserTest(bid, username, age) from User where bid in (?1)",  new ArrayList<>(){{add(2L);add(3L);}});
//        List<UserTest> userTest = userBaseRepository.find(UserTest.class, "select new com.sc.base.user.bean.pojo.UserTest(bid, username, age) from User where bid > :bid",  params);
        List<UserTest> userTest = userBaseRepository.find(UserTest.class, page, "select new com.sc.base.user.bean.pojo.UserTest(bid, username, age) from User where bid > :bid",  params);

        System.out.println(userTest);
//        UserTest userTest = userBaseRepository.findEntity("select new com.sc.base.user.bean.pojo.UserTest(bid, username, age) from User where bid = :bid", new HashMap<>(){{
//            put("bid", 2L);
//        }});
    }

    @Transactional()
    public void execute() {

//        int execute = userBaseRepository.execute("update User set deleted = 0");
//        System.out.println(execute);

//        int execute = userBaseRepository.execute("delete User where deleted = ?1 and bid > ?2 ", false, 3L);
//        System.out.println(execute);

//        Map<String, Object> params = new HashMap<>();
//        params.put("deleted", false);
//        params.put("maxBid", 4L);
//        params.put("reg", LocalDateTime.now());
//        int execute = userBaseRepository.execute("update User set regDateTime = :reg where deleted = :deleted and bid < :maxBid", params);
//        System.out.println(execute);


    }

    @Transactional(readOnly = true)
    public void count() {
//        Long count = userBaseRepository.count("select count(1) from User");
//        System.out.println(count);
//
//        Long count = userBaseRepository.count("select count(1) from User where bid > ?1", 2L);
//        System.out.println(count);

//        Map<String, Object> params = new HashMap<>();
//        params.put("bid", 2L);
//        Long count = userBaseRepository.count("select count(1) from User where bid > :bid", params);
//        System.out.println(count);


    }

    @Transactional(readOnly = true)
    public void jpqlUtils() {
        UserTest u = new UserTest();
        u.setUsername("马");
        u.setBid(1L);

        try {

            userBaseRepository.find(JpaQueryUtils.builder(UserTest.class)
                    .select("new com.sc.base.user.bean.pojo.UserTest(bid, username, age)")
                    .from("User")
                    .ge("bid", u.getBid())
                    .startLike("username", u.getUsername())
                    .eq("age", u.getAge())
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
